package com.parking.carsystem.parkingchargesystem.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.adapter.MyCustomPagerAdapter;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.fragment.ChooseLisensePlateFragment;
import com.parking.carsystem.parkingchargesystem.fragment.FragmentFactory;
import com.parking.carsystem.parkingchargesystem.fragment.InputLisensePlateFragment;
import com.parking.carsystem.parkingchargesystem.listener.OnclickItemPlateNumber;
import com.parking.carsystem.parkingchargesystem.view.NoScrollViewPager;

import java.util.ArrayList;

import butterknife.BindView;

public class ChooseLisencePlateActivity extends BaseActivity implements View.OnClickListener, OnclickItemPlateNumber {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.lisence_plate_message)
    NoScrollViewPager lisencePlateMessage;
    @BindView(R.id.table_layout)
    TabLayout tableLayout;
    @BindView(R.id.title_bg)
    RelativeLayout title_bg;
    private MyCustomPagerAdapter pagerAdapter;
    private InputLisensePlateFragment inputLisensePlateFragment;
    private ChooseLisensePlateFragment chooseLisensePlateFragment;
    private ArrayList<String> titliText;
    private int parkingType;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_choose_lisence_plate;
    }

    @Override
    public void initView() {
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        lisencePlateMessage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initData() {
        text.setText("选择车牌");
        if (titliText == null) {
            titliText = new ArrayList<>();
            titliText.add("选择车牌");
            titliText.add("输入车牌");
        }
        String payment = getIntent().getStringExtra(Constant.SELFSERVICEPAYMENT);
        pagerAdapter = new MyCustomPagerAdapter(getSupportFragmentManager(),titliText);
        lisencePlateMessage.setAdapter(pagerAdapter);
        tableLayout.setupWithViewPager(lisencePlateMessage);
        if (!TextUtils.isEmpty(payment)) {
            chooseLisensePlateFragment = (ChooseLisensePlateFragment) FragmentFactory.getFragmentInsance(0);
            chooseLisensePlateFragment.setPayMent(payment);
            inputLisensePlateFragment = (InputLisensePlateFragment) FragmentFactory.getFragmentInsance(1);
            inputLisensePlateFragment.setPayMent(payment);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (lisencePlateMessage.getCurrentItem() == 1) {
            if (inputLisensePlateFragment == null) {
                inputLisensePlateFragment = (InputLisensePlateFragment) FragmentFactory.getFragmentInsance(1);
            }
            if (inputLisensePlateFragment.getKeyDownVisible()) {
                inputLisensePlateFragment.hidleOnkeyDown();
            } else {
                finish();
            }
        } else {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (Constant.ADDPARKINGRESULTCODE == resultCode) {
            if (chooseLisensePlateFragment == null) {
                chooseLisensePlateFragment = (ChooseLisensePlateFragment) FragmentFactory.getFragmentInsance(0);
            }
            chooseLisensePlateFragment.getGroupList();
        }
    }

    @Override
    public void choosePlateNumber(String plateNumber) {

    }
}
